#include "ringQueue.hpp"
#include <cstdlib>
#include <time.h>

void* producer(void* args)//生产者
{
    RingQueue<int>* rq = (RingQueue<int>*)args;
    while(true)
    {
        int a = rand()%1000;
        rq->push(a);
        std::cout << "生成一个数据 " << a << std::endl;
    }
}

void* consumer(void* args)//消费者
{
    RingQueue<int>* rq = (RingQueue<int>*)args;
    while(true)
    {
        sleep(1);
        int a;
        rq->pop(&a);
        std::cout << "消费一个数据 " << a << std::endl;
    }
   
}

int main()
{
    srand((unsigned long)time(nullptr) ^ 556622);
    pthread_t c[10],p[8];
    RingQueue<int> rq(10);
    for(int i = 0; i < 10; i++)
    {
        pthread_create(c+i,nullptr,producer,&rq);
    }
    for(int i = 0; i < 8; i++)
    {
        pthread_create(p+i,nullptr,consumer,&rq);
    }

    for(int i = 0; i < 10; i++)pthread_join(c[i],nullptr);
    for(int i = 0; i < 8; i++)pthread_join(p[i],nullptr);
    return 0;
}